﻿using cszmcaux;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace _8轴运动控制
{
    public class Zmotion
    {
        public Zmotion(string IPAdress = "127.0.0.1")
        {
            this.IPAdress = IPAdress;
        }

        private string IPAdress = string.Empty;
        private IntPtr handle;
        //private IntPtr handle1;
        //private IntPtr handle2;
        //private IntPtr handle3;
        //private IntPtr handle4;
        //private IntPtr handle5;
        //private IntPtr handle6;
        //private IntPtr handle7;
        //private IntPtr handle8;
        //private IntPtr handle9;
        //private IntPtr handle10;
        //private IntPtr handle11;
        //private IntPtr handle12;
        public bool InitedOK = false;

        //脉冲当量
        public float unin0 = 2000;
        public float unin1 = 2000;
        public float unin2 = 2000;
        public float unin3 = 2000;
        public float unin4 = 8000;
        public float unin5 = 1800;
        public float unin6 = 2000;
        public float unin7 = 10000;

        //停止所有轴标志
        public bool AllaxisStop = false;


        /// <summary>
        /// 初始化板卡
        /// </summary>
        /// <returns></returns>
        public OperationResult IniCard()
        {

            int error = zmcaux.ZAux_OpenEth(IPAdress, out handle);
            //int error1 = zmcaux.ZAux_OpenEth(IPAdress, out handle1);
            //error1+= zmcaux.ZAux_OpenEth(IPAdress, out handle2);
            //Thread.Sleep(100);
            //error1+= zmcaux.ZAux_OpenEth(IPAdress, out handle3);
            //Thread.Sleep(100);
            //error1 += zmcaux.ZAux_OpenEth(IPAdress, out handle4);
            //Thread.Sleep(100);
            //error1+= zmcaux.ZAux_OpenEth(IPAdress, out handle5);
            //Thread.Sleep(100);
            //error1+= zmcaux.ZAux_OpenEth(IPAdress, out handle6);
            //Thread.Sleep(100);
            //error1+= zmcaux.ZAux_OpenEth(IPAdress, out handle7);
            //Thread.Sleep(100);
            //error1+= zmcaux.ZAux_OpenEth(IPAdress, out handle8);
            //Thread.Sleep(100);
            //error1+= zmcaux.ZAux_OpenEth(IPAdress, out handle9);
            //Thread.Sleep(100);
            //error1 += zmcaux.ZAux_OpenEth(IPAdress, out handle10);
            //Thread.Sleep(100);
            //error1+= zmcaux.ZAux_OpenEth(IPAdress, out handle11);
            //Thread.Sleep(100);
            //error1+= zmcaux.ZAux_OpenEth(IPAdress, out handle12);

            if (error == 0 && handle != IntPtr.Zero)
            {
                InitedOK = true;
                return OperationResult.CreatSuccessResult();
            }
            else
            {
                InitedOK = false;
                return OperationResult.CreatFailResult();
            }
            


        }
        /// <summary>
        /// 关闭板卡
        /// </summary>
        /// <returns></returns>
        public OperationResult CloseCard()
        {
            int ret = zmcaux.ZAux_Close(handle);
            if (ret == 0)
            {
                handle = IntPtr.Zero;
                InitedOK = false;
                return OperationResult.CreatSuccessResult();
            }
            else
            {
                return OperationResult.CreatFailResult();
            }
        }

        /// <summary>
        /// 设置脉冲当量
        /// </summary>
        /// <returns></returns>
        public OperationResult SetUnits(int axis)
        {
            int error = 0;
            try
            {
                //设置脉冲当量
                switch (axis)
                {
                    case 0:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin0);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 1:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin1);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 2:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin2);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 3:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin3);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 4:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin4);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 5:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin5);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 6:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin6);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 7:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin7);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    default:
                        break;
                }
                if (error == 0)
                {
                    var result2 = OperationResult.CreatSuccessResult();
                    return result2;
                }
                else
                {
                    var result2 = OperationResult.CreatFailResult();
                    return result2;
                }

            }
            catch (Exception)
            {
                var result2 = OperationResult.CreatFailResult();
                return result2;
            }





        }

        public OperationResult SetUintsIni(int axis,float unit)
        {
            var result = CommonInitValidate();
            if (result.IsSuccess == false) return result;
            int error = 0;

            //设置脉冲当量
            try
            {
                error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unit);
                ErrorHandler("ZAux_Direct_SetUnits", error);
                return OperationResult.CreatSuccessResult();
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.ErrorMsg = ex.Message;
                return result;
            }
            


        }


        /// <summary>
        /// 连续运动
        /// </summary>
        /// <param name="axis">轴号</param>
        /// <param name="vel">运行速度</param>
        /// <param name="dir">方向</param>
        /// <param name="velmin">最小速度</param>
        /// <param name="acc">加速度</param>
        /// <param name="dec">减速度</param>
        /// <param name="sramp">S曲线时间</param>
        /// <returns>操作结果</returns>
        public OperationResult VMove(short axis, float vel, bool dir, float velmin, float acc, float dec, float sramp)
        {
            //判断是否满足运动条件
            var result = CommonMotionValidate(axis);
            if (result.IsSuccess == false) return result;
            //创建错误码
            int error = 0;
            try
            {
                //设置轴参数
                /*
                    * 0 虚拟轴。
                    1 脉冲方向方式的步进或伺服 。
                    2 模拟信号控制方式的伺服 。
                    3 正交编码器 。
                    4 脉冲方向输出+正交编码器输入。
                    5 脉冲方向输出+脉冲方向编码器输入
                    6 脉冲方向方式的编码器，可用于手轮输入。
                    7 脉冲方向方式步进或伺服+EZ 信号输入。
                    8 ZCAN 扩展脉冲方向方式步进或伺服 。
                    9 ZCAN 扩展正交编码器。
                    10 ZCAN 扩展脉冲方向方式的编码器
                 */
                //error = zmcaux.ZAux_Direct_SetAtype(handle, axis, 1);
                //ErrorHandler("ZAux_Direct_SetAtype", error);
                //设置脉冲当量
                switch (axis)
                {
                    case 0:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin0);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 1:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin1);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 2:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin2);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 3:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin3);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 4:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin4);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 5:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin5);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 6:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin6);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 7:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin7);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    default:
                        break;
                }
                //设置最小速度
                //error = zmcaux.ZAux_Direct_SetLspeed(handle, axis, velmin);
                //ErrorHandler("ZAux_Direct_SetLspeed", error);
                //设置运行速度
                error = zmcaux.ZAux_Direct_SetSpeed(handle, axis, vel);
                ErrorHandler("ZAux_Direct_SetSpeed", error);
                //设置加速度
                error = zmcaux.ZAux_Direct_SetAccel(handle, axis, acc);
                ErrorHandler("ZAux_Direct_SetAccel", error);
                //设置减速度
                error = zmcaux.ZAux_Direct_SetDecel(handle, axis, dec);
                ErrorHandler("ZAux_Direct_SetDecel", error);
                //设置S曲线时间，让运动更平滑
                error = zmcaux.ZAux_Direct_SetSramp(handle, axis, sramp);
                ErrorHandler("ZAux_Direct_SetSramp", error);
                //设置方向并运动
                error = zmcaux.ZAux_Direct_Single_Vmove(handle, axis, dir ? 1 : -1);
                ErrorHandler("ZAux_Direct_Single_Vmove", error);
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.ErrorMsg = ex.Message;
                return result;
            }
            return OperationResult.CreatSuccessResult();

        }

        /// <summary>
        /// 相对运动
        /// </summary>
        /// <param name="axis"></param>
        /// <param name="vel"></param>
        /// <param name="distance"></param>
        /// <param name="velmin"></param>
        /// <param name="acc"></param>
        /// <param name="dec"></param>
        /// <param name="sramp"></param>
        /// <returns></returns>
        public OperationResult MoveRelative(short axis, float vel, float distance, float velmin, float acc, float dec, float sramp)
        {
            //判断是否满足运动条件
            var result = CommonMotionValidate(axis);
            if (result.IsSuccess == false) return result;
            //创建错误码
            int error = 0;
            try
            {
                //设置轴参数
                /*
                    * 0 虚拟轴。
                    1 脉冲方向方式的步进或伺服 。
                    2 模拟信号控制方式的伺服 。
                    3 正交编码器 。
                    4 脉冲方向输出+正交编码器输入。
                    5 脉冲方向输出+脉冲方向编码器输入
                    6 脉冲方向方式的编码器，可用于手轮输入。
                    7 脉冲方向方式步进或伺服+EZ 信号输入。
                    8 ZCAN 扩展脉冲方向方式步进或伺服 。
                    9 ZCAN 扩展正交编码器。
                    10 ZCAN 扩展脉冲方向方式的编码器
                 */
                //error = zmcaux.ZAux_Direct_SetAtype(handle, axis, 1);
                //ErrorHandler("ZAux_Direct_SetAtype", error);
                //设置脉冲当量
                switch (axis)
                {
                    case 0:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin0);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 1:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin1);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 2:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin2);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 3:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin3);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 4:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin4);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 5:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin5);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 6:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin6);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 7:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin7);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    default:
                        break;
                }
                //设置最小速度
                //error = zmcaux.ZAux_Direct_SetLspeed(handle, axis, velmin);
                //ErrorHandler("ZAux_Direct_SetLspeed", error);
                //设置运行速度
                error = zmcaux.ZAux_Direct_SetSpeed(handle, axis, vel);
                ErrorHandler("ZAux_Direct_SetSpeed", error);
                //设置加速度
                error = zmcaux.ZAux_Direct_SetAccel(handle, axis, acc);
                ErrorHandler("ZAux_Direct_SetAccel", error);
                //设置减速度
                error = zmcaux.ZAux_Direct_SetDecel(handle, axis, dec);
                ErrorHandler("ZAux_Direct_SetDecel", error);
                //设置S曲线时间，让运动更平滑
                error = zmcaux.ZAux_Direct_SetSramp(handle, axis, sramp);
                ErrorHandler("ZAux_Direct_SetSramp", error);
                //设置方向并运动 (相对运动)
                error = zmcaux.ZAux_Direct_Single_Move(handle, axis, distance);
                ErrorHandler("ZAux_Direct_Single_Move", error);
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.ErrorMsg = ex.Message;
                return result;
            }
            return OperationResult.CreatSuccessResult();
        }


        /// <summary>
        /// 绝对运动
        /// </summary>
        /// <param name="axis"></param>
        /// <param name="vel"></param>
        /// <param name="pos"></param>
        /// <param name="velmin"></param>
        /// <param name="acc"></param>
        /// <param name="dec"></param>
        /// <param name="sramp"></param>
        /// <returns></returns>
        public OperationResult MoveABS(short axis, float vel, float pos, float velmin, float acc, float dec, float sramp)
        {
            //判断是否满足运动条件
            var result = CommonMotionValidate(axis);
            if (result.IsSuccess == false) return result;
            //创建错误码
            int error = 0;
            try
            {
                //设置轴参数
                /*
                    * 0 虚拟轴。
                    1 脉冲方向方式的步进或伺服 。
                    2 模拟信号控制方式的伺服 。
                    3 正交编码器 。
                    4 脉冲方向输出+正交编码器输入。
                    5 脉冲方向输出+脉冲方向编码器输入
                    6 脉冲方向方式的编码器，可用于手轮输入。
                    7 脉冲方向方式步进或伺服+EZ 信号输入。
                    8 ZCAN 扩展脉冲方向方式步进或伺服 。
                    9 ZCAN 扩展正交编码器。
                    10 ZCAN 扩展脉冲方向方式的编码器
                 */
                //error = zmcaux.ZAux_Direct_SetAtype(handle, axis, 1);
                //ErrorHandler("ZAux_Direct_SetAtype", error);
                //设置脉冲当量
                switch (axis)
                {
                    case 0:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin0);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 1:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin1);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 2:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin2);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 3:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin3);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 4:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin4);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 5:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin5);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 6:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin6);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 7:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin7);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    default:
                        break;
                }
                //设置最小速度
                //error = zmcaux.ZAux_Direct_SetLspeed(handle, axis, velmin);
                //ErrorHandler("ZAux_Direct_SetLspeed", error);
                //设置运行速度
                error = zmcaux.ZAux_Direct_SetSpeed(handle, axis, vel);
                ErrorHandler("ZAux_Direct_SetSpeed", error);
                //设置加速度
                error = zmcaux.ZAux_Direct_SetAccel(handle, axis, acc);
                ErrorHandler("ZAux_Direct_SetAccel", error);
                //设置减速度
                error = zmcaux.ZAux_Direct_SetDecel(handle, axis, dec);
                ErrorHandler("ZAux_Direct_SetDecel", error);
                //设置S曲线时间，让运动更平滑
                error = zmcaux.ZAux_Direct_SetSramp(handle, axis, sramp);
                ErrorHandler("ZAux_Direct_SetSramp", error);
                //设置方向并运动 (相对运动)
                error = zmcaux.ZAux_Direct_Single_MoveAbs(handle, axis, pos);
                ErrorHandler("ZAux_Direct_Single_MoveAbs", error);
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.ErrorMsg = ex.Message;
                return result;
            }
            return OperationResult.CreatSuccessResult();
        }

        /// <summary>
        /// 2轴相对定位
        /// </summary>
        /// <param name="axis"></param>
        /// <param name="vel"></param>
        /// <param name="distance"></param>
        /// <param name="velmin"></param>
        /// <param name="acc"></param>
        /// <param name="dec"></param>
        /// <param name="sramp"></param>
        /// <returns></returns>
        public OperationResult Move2DRelative(short[] axis, float[] vel, float[] distance, float[] velmin, float[] acc, float[] dec, float[] sramp)
        {
            if (axis.Length == 2 && vel.Length == 2 && distance.Length == 2 && velmin.Length == 2 && acc.Length == 2 && dec.Length == 2 && sramp.Length == 2)
            {
                OperationResult result = new OperationResult();
                for (int i = 0; i < 2; i++)
                {
                    result = MoveRelative(axis[i], vel[i], distance[i], velmin[i], acc[i], dec[i], sramp[i]);
                    if (!result.IsSuccess) return result;
                }
                for (int i = 0; i < 2; i++)
                {
                    result = WaitStop(axis[i]);
                    if (!result.IsSuccess) return result;
                }
                return OperationResult.CreatSuccessResult();
            }
            return new OperationResult()
            {
                IsSuccess = false,
                ErrorMsg = "传递参数长度不正确"

            };

        }


        /// <summary>
        /// 2轴绝对定位
        /// </summary>
        /// <param name="axis"></param>
        /// <param name="vel"></param>
        /// <param name="distance"></param>
        /// <param name="velmin"></param>
        /// <param name="acc"></param>
        /// <param name="dec"></param>
        /// <param name="sramp"></param>
        /// <returns></returns>
        public OperationResult Move2DABS(short[] axis, float[] vel, float[] pos, float[] velmin, float[] acc, float[] dec, float[] sramp)
        {
            if (axis.Length == 2 && vel.Length == 2 && pos.Length == 2 && velmin.Length == 2 && acc.Length == 2 && dec.Length == 2 && sramp.Length == 2)
            {
                OperationResult result = new OperationResult();
                for (int i = 0; i < 2; i++)
                {
                    result = MoveABS(axis[i], vel[i], pos[i], velmin[i], acc[i], dec[i], sramp[i]);
                    if (!result.IsSuccess) return result;
                }
                for (int i = 0; i < 2; i++)
                {
                    result = WaitStop(axis[i]);
                    if (!result.IsSuccess) return result;
                }
                return OperationResult.CreatSuccessResult();
            }
            return new OperationResult()
            {
                IsSuccess = false,
                ErrorMsg = "传递参数长度不正确"

            };

        }

        /// <summary>
        /// 3轴相对定位
        /// </summary>
        /// <param name="axis"></param>
        /// <param name="vel"></param>
        /// <param name="distance"></param>
        /// <param name="velmin"></param>
        /// <param name="acc"></param>
        /// <param name="dec"></param>
        /// <param name="sramp"></param>
        /// <returns></returns>
        public OperationResult Move3DRelative(short[] axis, float[] vel, float[] distance, float[] velmin, float[] acc, float[] dec, float[] sramp)
        {

            if (axis.Length == 3 && vel.Length == 3 && distance.Length == 3 && velmin.Length == 3 && acc.Length == 3 && dec.Length == 3 && sramp.Length == 3)
            {
                OperationResult result = new OperationResult();

                //2轴相对定位
                result = Move2DRelative(new short[] { axis[0], axis[1] }, new float[] { vel[0], vel[1] }, new float[] { distance[0], distance[1] }, new float[] { velmin[0], velmin[1] }
                , new float[] { acc[0], acc[1] }, new float[] { dec[0], dec[1] }, new float[] { sramp[0], sramp[1] });
                if (!result.IsSuccess) return result;
                if (AllaxisStop)
                {
                    return OperationResult.CreatSuccessResult();
                }
                //再走Z轴相对定位
                result = MoveRelative(axis[2], vel[2], distance[2], velmin[2], acc[2], dec[2], sramp[2]);
                if (!result.IsSuccess) return result;
                //等待停止
                result = WaitStop(axis[2]);
                if (!result.IsSuccess) return result;
                return OperationResult.CreatSuccessResult();
            }
            return new OperationResult()
            {
                IsSuccess = false,
                ErrorMsg = "传递参数长度不正确"

            };

        }

        /// <summary>
        /// 3轴绝对定位
        /// </summary>
        /// <param name="axis"></param>
        /// <param name="vel"></param>
        /// <param name="distance"></param>
        /// <param name="velmin"></param>
        /// <param name="acc"></param>
        /// <param name="dec"></param>
        /// <param name="sramp"></param>
        /// <returns></returns>
        public OperationResult Move3DABS(short[] axis, float[] vel, float[] pos, float[] velmin, float[] acc, float[] dec, float[] sramp)
        {

            if (axis.Length == 3 && vel.Length == 3 && pos.Length == 3 && velmin.Length == 3 && acc.Length == 3 && dec.Length == 3 && sramp.Length == 3)
            {
                OperationResult result = new OperationResult();
                //先动Z轴会原点（安全位置） ，默认X轴是0，Y轴是1，Z轴是2
                result = MoveABS(axis[2], vel[2], 0.0f, velmin[2], acc[2], dec[2], sramp[2]);
                if (!result.IsSuccess) return result;
                //等待停止
                result = WaitStop(axis[2]);
                if (!result.IsSuccess) return result;
                if (AllaxisStop)
                {
                    return OperationResult.CreatSuccessResult();
                }

                //2轴绝对定位
                result = Move2DABS(new short[] { axis[0], axis[1] }, new float[] { vel[0], vel[1] }, new float[] { pos[0], pos[1] }, new float[] { velmin[0], velmin[1] }
                , new float[] { acc[0], acc[1] }, new float[] { dec[0], dec[1] }, new float[] { sramp[0], sramp[1] });
                if (!result.IsSuccess) return result;
                //再走Z轴绝对定位
                result = MoveABS(axis[2], vel[2], pos[2], velmin[2], acc[2], dec[2], sramp[2]);
                if (!result.IsSuccess) return result;
                //等待停止
                result = WaitStop(axis[2]);
                if (!result.IsSuccess) return result;
                return OperationResult.CreatSuccessResult();
            }
            return new OperationResult()
            {
                IsSuccess = false,
                ErrorMsg = "传递参数长度不正确"

            };

        }


        /// <summary>
        /// 多轴相对直线插补
        /// </summary>
        /// <param name="axis"></param>
        /// <param name="distance"></param>
        /// <param name="vel"></param>
        /// <param name="acc"></param>
        /// <param name="dec"></param>
        /// <returns></returns>
        public OperationResult MoveLineRelative(int[] axis, float[] distance, float vel, float acc, float dec)
        {
            if (axis.Length >= 2 && axis.Length == distance.Length)
            {
                OperationResult result = new OperationResult();
                //判断每个轴是否满足要求
                foreach (var item in axis)
                {
                    result = CommonMotionValidate((short)item);
                    if (result.IsSuccess == false) return result;

                }
                //设置脉冲当量
                for (int i = 0; i < axis.Length; i++)
                {
                    result = SetUnits(axis[i]);
                    if (result.IsSuccess == false) return result;
                }

                int error = 0;

                try
                {
                    //选择base轴列表
                    error = zmcaux.ZAux_Direct_Base(handle, axis.Length, axis);
                    ErrorHandler("ZAux_Direct_Base", error);
                    //插补运动使用的是主轴参数，及BASE的第一个轴
                    zmcaux.ZAux_Direct_SetSpeed(handle, axis[0], vel);
                    ErrorHandler("ZAux_Direct_SetSpeed", error);
                    zmcaux.ZAux_Direct_SetAccel(handle, axis[0], acc);
                    ErrorHandler("ZAux_Direct_SetAccel", error);
                    zmcaux.ZAux_Direct_SetDecel(handle, axis[0], dec);
                    ErrorHandler("ZAux_Direct_SetDecel", error);

                    zmcaux.ZAux_Direct_Move(handle, axis.Length, axis, distance);
                    ErrorHandler("ZAux_Direct_Move", error);

                    return OperationResult.CreatSuccessResult();
                }
                catch (Exception ex)
                {

                    result.IsSuccess = false;
                    result.ErrorMsg = ex.Message;
                    return result;
                }

            }
            return new OperationResult()
            {
                IsSuccess = false,
                ErrorMsg = "传递参数不正确"
            };
        }

        /// <summary>
        /// 多轴绝对直线插补
        /// </summary>
        /// <param name="axis"></param>
        /// <param name="distance"></param>
        /// <param name="vel"></param>
        /// <param name="acc"></param>
        /// <param name="dec"></param>
        /// <returns></returns>
        public OperationResult MoveLineABS(int[] axis, float[] pos, float vel, float acc, float dec)
        {
            if (axis.Length >= 2 && axis.Length == pos.Length)
            {
                OperationResult result = new OperationResult();
                //判断每个轴是否满足要求
                foreach (var item in axis)
                {
                    result = CommonMotionValidate((short)item);
                    if (result.IsSuccess == false) return result;
                }
                //设置脉冲当量
                for (int i = 0; i < axis.Length; i++)
                {
                    result = SetUnits(axis[i]);
                    if (result.IsSuccess == false) return result;
                }
                int error = 0;
                try
                {
                    //选择base轴列表
                    error = zmcaux.ZAux_Direct_Base(handle, axis.Length, axis);
                    ErrorHandler("ZAux_Direct_Base", error);
                    //插补运动使用的是主轴参数，及BASE的第一个轴
                    zmcaux.ZAux_Direct_SetSpeed(handle, axis[0], vel);
                    ErrorHandler("ZAux_Direct_SetSpeed", error);
                    zmcaux.ZAux_Direct_SetAccel(handle, axis[0], acc);
                    ErrorHandler("ZAux_Direct_SetAccel", error);
                    zmcaux.ZAux_Direct_SetDecel(handle, axis[0], dec);
                    ErrorHandler("ZAux_Direct_SetDecel", error);

                    zmcaux.ZAux_Direct_MoveAbs(handle, axis.Length, axis, pos);
                    ErrorHandler("ZAux_Direct_MoveABS", error);

                    return OperationResult.CreatSuccessResult();
                }
                catch (Exception ex)
                {

                    result.IsSuccess = false;
                    result.ErrorMsg = ex.Message;
                    return result;
                }

            }
            return new OperationResult()
            {
                IsSuccess = false,
                ErrorMsg = "传递参数不正确"
            };
        }

        /// <summary>
        /// 圆弧XY两轴相对插补(圆心方式)
        /// </summary>
        /// <param name="axis"></param>
        /// <param name="distance"></param>
        /// <param name="circleCenter"></param>
        /// <param name="vel"></param>
        /// <param name="acc"></param>
        /// <param name="dec"></param>
        /// <param name="dir"></param>
        /// <returns></returns>
        public OperationResult MoveCircleRealitive(int[] axis, float[] distance, float[] circleCenter, float vel, float acc, float dec, int dir)
        {
            if (axis.Length == 2 && axis.Length == distance.Length)
            {
                OperationResult result = new OperationResult();
                //判断每个轴是否满足要求
                foreach (var item in axis)
                {
                    result = CommonMotionValidate((short)item);
                    if (result.IsSuccess == false) return result;
                }
                //设置脉冲当量
                for (int i = 0; i < axis.Length; i++)
                {
                    result = SetUnits(axis[i]);
                    if (result.IsSuccess == false) return result;
                }
                int error = 0;
                try
                {
                    //选择base轴列表
                    error = zmcaux.ZAux_Direct_Base(handle, axis.Length, axis);
                    ErrorHandler("ZAux_Direct_Base", error);
                    //插补运动使用的是主轴参数，及BASE的第一个轴
                    zmcaux.ZAux_Direct_SetSpeed(handle, axis[0], vel);
                    ErrorHandler("ZAux_Direct_SetSpeed", error);
                    zmcaux.ZAux_Direct_SetAccel(handle, axis[0], acc);
                    ErrorHandler("ZAux_Direct_SetAccel", error);
                    zmcaux.ZAux_Direct_SetDecel(handle, axis[0], dec);
                    ErrorHandler("ZAux_Direct_SetDecel", error);

                    zmcaux.ZAux_Direct_MoveCirc(handle, axis.Length, axis, distance[0], distance[1], circleCenter[0], circleCenter[1], dir);
                    ErrorHandler("ZAux_Direct_MoveCirc", error);

                    return OperationResult.CreatSuccessResult();
                }
                catch (Exception ex)
                {

                    result.IsSuccess = false;
                    result.ErrorMsg = ex.Message;
                    return result;
                }

            }
            return new OperationResult()
            {
                IsSuccess = false,
                ErrorMsg = "传递参数不正确"
            };
        }

        /// <summary>
        /// 圆弧XY两轴绝对插补(圆心方式)
        /// </summary>
        /// <param name="axis"></param>
        /// <param name="distance"></param>
        /// <param name="circleCenter"></param>
        /// <param name="vel"></param>
        /// <param name="acc"></param>
        /// <param name="dec"></param>
        /// <param name="dir"></param>
        /// <returns></returns>
        public OperationResult MoveCircleABS(int[] axis, float[] distance, float[] circleCenter, float vel, float acc, float dec, int dir)
        {
            if (axis.Length == 2 && axis.Length == distance.Length)
            {
                OperationResult result = new OperationResult();
                //判断每个轴是否满足要求
                foreach (var item in axis)
                {
                    result = CommonMotionValidate((short)item);
                    if (result.IsSuccess == false) return result;
                }
                //设置脉冲当量
                for (int i = 0; i < axis.Length; i++)
                {
                    result = SetUnits(axis[i]);
                    if (result.IsSuccess == false) return result;
                }
                int error = 0;
                try
                {
                    //选择base轴列表
                    error = zmcaux.ZAux_Direct_Base(handle, axis.Length, axis);
                    ErrorHandler("ZAux_Direct_Base", error);
                    //插补运动使用的是主轴参数，及BASE的第一个轴
                    zmcaux.ZAux_Direct_SetSpeed(handle, axis[0], vel);
                    ErrorHandler("ZAux_Direct_SetSpeed", error);
                    zmcaux.ZAux_Direct_SetAccel(handle, axis[0], acc);
                    ErrorHandler("ZAux_Direct_SetAccel", error);
                    zmcaux.ZAux_Direct_SetDecel(handle, axis[0], dec);
                    ErrorHandler("ZAux_Direct_SetDecel", error);

                    zmcaux.ZAux_Direct_MoveCircAbs(handle, axis.Length, axis, distance[0], distance[1], circleCenter[0], circleCenter[1], dir);
                    ErrorHandler("ZAux_Direct_MoveCircABS", error);

                    return OperationResult.CreatSuccessResult();
                }
                catch (Exception ex)
                {

                    result.IsSuccess = false;
                    result.ErrorMsg = ex.Message;
                    return result;
                }

            }
            return new OperationResult()
            {
                IsSuccess = false,
                ErrorMsg = "传递参数不正确"
            };
        }


        /// <summary>
        /// 圆弧XY两轴相对插补(中间点方式)
        /// </summary>
        /// <param name="axis"></param>
        /// <param name="distance"></param>
        /// <param name="midpos"></param>
        /// <param name="vel"></param>
        /// <param name="acc"></param>
        /// <param name="dec"></param>
        /// <param name="dir"></param>
        /// <returns></returns>
        public OperationResult MoveCircle2Realitive(int[] axis, float[] distance, float[] midpos, float vel, float acc, float dec)
        {
            if (axis.Length == 2 && axis.Length == distance.Length)
            {
                OperationResult result = new OperationResult();
                //判断每个轴是否满足要求
                foreach (var item in axis)
                {
                    result = CommonMotionValidate((short)item);
                    if (result.IsSuccess == false) return result;
                }
                //设置脉冲当量
                for (int i = 0; i < axis.Length; i++)
                {
                    result = SetUnits(axis[i]);
                    if (result.IsSuccess == false) return result;
                }
                int error = 0;
                try
                {
                    //选择base轴列表
                    error = zmcaux.ZAux_Direct_Base(handle, axis.Length, axis);
                    ErrorHandler("ZAux_Direct_Base", error);
                    //插补运动使用的是主轴参数，及BASE的第一个轴
                    zmcaux.ZAux_Direct_SetSpeed(handle, axis[0], vel);
                    ErrorHandler("ZAux_Direct_SetSpeed", error);
                    zmcaux.ZAux_Direct_SetAccel(handle, axis[0], acc);
                    ErrorHandler("ZAux_Direct_SetAccel", error);
                    zmcaux.ZAux_Direct_SetDecel(handle, axis[0], dec);
                    ErrorHandler("ZAux_Direct_SetDecel", error);

                    zmcaux.ZAux_Direct_MoveCirc2(handle, axis.Length, axis, midpos[0], midpos[1], distance[0], distance[1]);
                    ErrorHandler("ZAux_Direct_MoveCirc2", error);

                    return OperationResult.CreatSuccessResult();
                }
                catch (Exception ex)
                {

                    result.IsSuccess = false;
                    result.ErrorMsg = ex.Message;
                    return result;
                }

            }
            return new OperationResult()
            {
                IsSuccess = false,
                ErrorMsg = "传递参数不正确"
            };
        }

        /// <summary>
        /// 圆弧XY两轴绝对插补(中间点方式)
        /// </summary>
        /// <param name="axis"></param>
        /// <param name="distance"></param>
        /// <param name="midpos"></param>
        /// <param name="vel"></param>
        /// <param name="acc"></param>
        /// <param name="dec"></param>
        /// <param name="dir"></param>
        /// <returns></returns>
        public OperationResult MoveCircle2ABS(int[] axis, float[] distance, float[] midpos, float vel, float acc, float dec)
        {
            if (axis.Length == 2 && axis.Length == distance.Length)
            {
                OperationResult result = new OperationResult();
                //判断每个轴是否满足要求
                foreach (var item in axis)
                {
                    result = CommonMotionValidate((short)item);
                    if (result.IsSuccess == false) return result;
                }
                //设置脉冲当量
                for (int i = 0; i < axis.Length; i++)
                {
                    result = SetUnits(axis[i]);
                    if (result.IsSuccess == false) return result;
                }
                int error = 0;
                try
                {
                    //选择base轴列表
                    error = zmcaux.ZAux_Direct_Base(handle, axis.Length, axis);
                    ErrorHandler("ZAux_Direct_Base", error);
                    //插补运动使用的是主轴参数，及BASE的第一个轴
                    zmcaux.ZAux_Direct_SetSpeed(handle, axis[0], vel);
                    ErrorHandler("ZAux_Direct_SetSpeed", error);
                    zmcaux.ZAux_Direct_SetAccel(handle, axis[0], acc);
                    ErrorHandler("ZAux_Direct_SetAccel", error);
                    zmcaux.ZAux_Direct_SetDecel(handle, axis[0], dec);
                    ErrorHandler("ZAux_Direct_SetDecel", error);

                    zmcaux.ZAux_Direct_MoveCirc2Abs(handle, axis.Length, axis, midpos[0], midpos[1], distance[0], distance[1]);
                    ErrorHandler("ZAux_Direct_MoveCirc2ABS", error);

                    return OperationResult.CreatSuccessResult();
                }
                catch (Exception ex)
                {

                    result.IsSuccess = false;
                    result.ErrorMsg = ex.Message;
                    return result;
                }

            }
            return new OperationResult()
            {
                IsSuccess = false,
                ErrorMsg = "传递参数不正确"
            };
        }


        /// <summary>
        /// 通用回零
        /// </summary>
        /// <param name="axis">轴号</param>
        /// <param name="vel">速度</param>
        /// <param name="creep">爬行速度</param>
        /// <param name="velmin">最小速度</param>
        /// <param name="homeio">原点IO点</param>
        /// <param name="acc">加速度</param>
        /// <param name="dec">减速度</param>
        /// <param name="sramp">S曲线时间</param>
        /// <param name="distance">原限距离</param>
        /// <param name="homeMode">回零模式</param>
        /// <returns>返回结果</returns>
        public OperationResult ZeroAxis(short axis, float vel, float creep, float velmin, int homeio, float acc, float dec, float sramp, float distance, int homeMode = 4)
        {
            //判断是否满足运动条件
            var result = CommonMotionValidate(axis);
            if (result.IsSuccess == false) return result;

            //先往负方向一直走，直到碰到负限位
            result = MoveRelative(axis, vel, -500, velmin, acc, dec, sramp);
            if (result.IsSuccess == false) return result;
            //等待停止
            result = WaitStop(axis);
            if (result.IsSuccess == false) return result;
            if (AllaxisStop)
            {
                return OperationResult.CreatSuccessResult();
            }
            //位置清零
            result = ZeroPos(axis);
            if (result.IsSuccess == false) return result;
            //往反方向走一段距离，超过原点和限位的距离
            result = MoveRelative(axis, vel, distance, velmin, acc, dec, sramp);
            if (result.IsSuccess == false) return result;
            //等待停止
            result = WaitStop(axis);
            if (AllaxisStop)
            {
                return OperationResult.CreatSuccessResult();
            }
            if (result.IsSuccess == false) return result;

            //再直接回零
            result = DirectZeroAxis(axis, vel, creep, velmin, homeio, acc, dec, homeMode);
            if (result.IsSuccess == false) return result;
            //等待回零停止
            result = WaitHomeStop(axis);
            if (result.IsSuccess == false) return result;

            return OperationResult.CreatSuccessResult();


        }


        /// <summary>
        /// 直接回原点
        /// </summary>
        /// <param name="axis">轴号</param>
        /// <param name="vel">速度</param>
        /// <param name="creep">爬行速度</param>
        /// <param name="velmin">最小速度</param>
        /// <param name="homeio">原点IO点</param>
        /// <param name="acc">加速度</param>
        /// <param name="dec">减速度</param>
        /// <param name="homeMode">回零模式</param>
        /// <returns>返回结果</returns>
        public OperationResult DirectZeroAxis(short axis, float vel, float creep, float velmin, int homeio, float acc, float dec, int homeMode = 4)
        {
            //判断是否满足运动条件
            var result = CommonMotionValidate(axis);
            if (result.IsSuccess == false) return result;
            //创建错误码
            int error = 0;
            try
            {
                //设置轴参数 
                //error = zmcaux.ZAux_Direct_SetAtype(handle, axis, 1);
                //ErrorHandler("ZAux_Direct_SetAtype", error);
                //设置脉冲当量
                switch (axis)
                {
                    case 0:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin0);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 1:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin1);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 2:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin2);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 3:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin3);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 4:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin4);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 5:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin5);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 6:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin6);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 7:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin7);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    default:
                        break;
                }
                //设置最小速度
                //error = zmcaux.ZAux_Direct_SetLspeed(handle, axis, velmin);
                //ErrorHandler("ZAux_Direct_SetLspeed", error);
                //设置运行速度
                error = zmcaux.ZAux_Direct_SetSpeed(handle, axis, vel);
                ErrorHandler("ZAux_Direct_SetSpeed", error);
                //设置加速度
                error = zmcaux.ZAux_Direct_SetAccel(handle, axis, acc);
                ErrorHandler("ZAux_Direct_SetAccel", error);
                //设置减速度
                error = zmcaux.ZAux_Direct_SetDecel(handle, axis, dec);
                ErrorHandler("ZAux_Direct_SetDecel", error);
                //设置爬行速度
                error = zmcaux.ZAux_Direct_SetCreep(handle, axis, creep);
                ErrorHandler("ZAux_Direct_SetCreep", error);
                //设置原点信号
                error = zmcaux.ZAux_Direct_SetDatumIn(handle, axis, homeio);
                ErrorHandler("ZAux_Direct_SetDatumIn", error);
                //执行回原点
                error = zmcaux.ZAux_Direct_Single_Datum(handle, axis, homeMode);
                ErrorHandler("ZAux_Direct_Single_Datum", error);
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.ErrorMsg = ex.Message;
                return result;
            }
            return OperationResult.CreatSuccessResult();
        }


        /// <summary>
        /// 等待停止
        /// </summary>
        /// <param name="axis"></param>
        /// <returns></returns>
        public OperationResult WaitStop(short axis)
        {
            var result = CommonInitValidate();
            if (result.IsSuccess == false) return result;
            int error = 0;
            int runstate = -1;
            do
            {
                //0 运动中， 1 未运动
                error = zmcaux.ZAux_Direct_GetIfIdle(handle, axis, ref runstate);
                ErrorHandler("ZAux_Direct_GetIfIdle", error);
                if (runstate == 0)
                {
                    Thread.Sleep(100);
                }
                else
                {
                    break;
                }
            } while (true);

            return OperationResult.CreatSuccessResult();
        }
        public OperationResult WaitStop(short axis1, short axis2)
        {
            var result = CommonInitValidate();
            if (result.IsSuccess == false) return result;
            int error = 0;
            int runstate1 = -1;
            int runstate2 = -1;
            do
            {
                error = zmcaux.ZAux_Direct_GetIfIdle(handle, axis1, ref runstate1);
                error = zmcaux.ZAux_Direct_GetIfIdle(handle, axis2, ref runstate2);
                ErrorHandler("ZAux_Direct_GetIfIdle", error);
                if (runstate1 == 0 || runstate2 == 0)
                {
                    Thread.Sleep(100);
                }
                else
                {
                    break;
                }
            } while (true);

            return OperationResult.CreatSuccessResult();
        }

        public OperationResult WaitStop(short axis1, short axis2, short axis3)
        {
            var result = CommonInitValidate();
            if (result.IsSuccess == false) return result;
            int error = 0;
            int runstate1 = -1;
            int runstate2 = -1;
            int runstate3 = -1;
            do
            {
                error = zmcaux.ZAux_Direct_GetIfIdle(handle, axis1, ref runstate1);
                error = zmcaux.ZAux_Direct_GetIfIdle(handle, axis2, ref runstate2);
                error = zmcaux.ZAux_Direct_GetIfIdle(handle, axis3, ref runstate3);
                ErrorHandler("ZAux_Direct_GetIfIdle", error);
                if (runstate1 == 0 || runstate2 == 0 || runstate3 == 0)
                {
                    Thread.Sleep(100);
                }
                else
                {
                    break;
                }
            } while (true);

            return OperationResult.CreatSuccessResult();
        }



        /// <summary>
        /// 等待回零停止
        /// </summary>
        /// <param name="axis"></param>
        /// <returns></returns>
        public OperationResult WaitHomeStop(short axis)
        {
            var result = CommonInitValidate();
            if (result.IsSuccess == false) return result;
            int error = 0;
            uint homestate = 0;
            do
            {
                error = zmcaux.ZAux_Direct_GetHomeStatus(handle, axis, ref homestate);
                ErrorHandler("ZAux_Direct_GetIfIdle", error);
            } while (homestate == 0);
            return OperationResult.CreatSuccessResult();
        }


        /// <summary>
        /// 反馈实时速度
        /// </summary>
        /// <param name="axis">轴号</param>
        /// <returns></returns>
        public float GetVel(short axis)
        {
            var result = CommonInitValidate();
            if (result.IsSuccess == false) return 0.0f;
            //定义速度
            float vel = 0.0f;
            //定义错误码
            int error = 0;
            try
            {
                error = zmcaux.ZAux_Direct_GetVpSpeed(handle, axis, ref vel);
                ErrorHandler("ZAux_Direct_GetVpSpeed", error);
                return vel;
            }
            catch (Exception)
            {
                return 0.0f;
            }



        }

        /// <summary>
        /// 获取规划位置
        /// </summary>
        /// <param name="axis"></param>
        /// <returns></returns>
        public float GetPos(short axis)
        {
            var result = CommonInitValidate();
            if (result.IsSuccess == false) return 0.0f;
            //定义当前位置
            float curpos = 0.0f;
            //定义错误码
            int error = 0;
            try
            {
                error = zmcaux.ZAux_Direct_GetDpos(handle, axis, ref curpos);
                ErrorHandler("ZAux_Direct_GetDpos", error);
                return curpos;
            }
            catch (Exception)
            {
                return 0.0f;
            }

        }
        /// <summary>
        /// 获取反馈位置
        /// </summary>
        /// <param name="axis"></param>
        /// <returns></returns>
        public float GetMos(short axis)
        {
            var result = CommonInitValidate();
            if (result.IsSuccess == false) return 0.0f;
            //定义当前位置
            float curpos = 0.0f;
            //定义错误码
            int error = 0;
            try
            {
                error = zmcaux.ZAux_Direct_GetMpos(handle, axis, ref curpos);
                ErrorHandler("ZAux_Direct_GetMpos", error);
                return curpos;
            }
            catch (Exception)
            {
                return 0.0f;
            }

        }

        /// <summary>
        /// 轴位置清零
        /// </summary>
        /// <param name="axis"></param>
        /// <returns></returns>
        public OperationResult ZeroPos(short axis)
        {
            var result = CommonInitValidate();
            if (result.IsSuccess == false) return result;

            //定义错误码
            int error = 0;
            try
            {
                error = zmcaux.ZAux_Direct_SetDpos(handle, axis, 0.0f);
                ErrorHandler("ZAux_Direct_GetDpos", error);

            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.ErrorMsg = ex.Message;
                return result;
            }

            return OperationResult.CreatSuccessResult();

        }
        /// <summary>
        /// 反馈位置清零
        /// </summary>
        /// <param name="axis"></param>
        /// <returns></returns>
        public OperationResult ZeroMpos(short axis)
        {
            var result = CommonInitValidate();
            if (result.IsSuccess == false) return result;

            //定义错误码
            int error = 0;
            try
            {
                error = zmcaux.ZAux_Direct_SetMpos(handle, axis, 0.0f);
                ErrorHandler("ZAux_Direct_SetMpos", error);

            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.ErrorMsg = ex.Message;
                return result;
            }

            return OperationResult.CreatSuccessResult();
        }


        /// <summary>
        /// 停止轴运动
        /// </summary>
        /// <param name="axis"></param>
        /// <returns></returns>
        public OperationResult StopAxis(short axis)
        {
            var result = CommonInitValidate();
            if (result.IsSuccess == false) return result;
            //定义错误码
            int error = 0;
            try
            {
                /*
             * 模式：
            0 （缺省）取消当前运动
            1 取消缓冲的运动
            2 取消当前运动和缓冲运动
            3 立即中断脉冲发送
             */
                error = zmcaux.ZAux_Direct_Single_Cancel(handle, axis, 2);
                ErrorHandler("ZAux_Direct_Single_Cancel", error);
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.ErrorMsg = ex.Message;
                return result;
            }
            return OperationResult.CreatSuccessResult();


        }

        /// <summary>
        /// 停止所有轴
        /// </summary>
        /// <param name="axis"></param>
        /// <returns></returns>
        public OperationResult StopAllAxis()
        {
            var result = CommonInitValidate();
            if (result.IsSuccess == false) return result;
            //定义错误码
            int error = 0;
            try
            {
                /*
             * 模式：
                0 取消当前运动
                1 取消缓冲的运动
                2 取消当前运动和缓冲运动
                3 立即停止
             */
                error = zmcaux.ZAux_Direct_CancelAxisList(handle, 8, new int[] { 0, 1, 2, 3, 4, 5, 6, 7 }, 2);
                ErrorHandler("ZAux_Direct_CancelAxisList", error);
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.ErrorMsg = ex.Message;
                return result;
            }
            return OperationResult.CreatSuccessResult();


        }

        /// <summary>
        /// 搜获IP地址集合
        /// </summary>
        /// <returns></returns>
        public List<String> GetEhList()
        {
            StringBuilder ipAddessList = new StringBuilder(100);
            uint addbufferlength = 1000;
            int error = zmcaux.ZAux_SearchEthlist(ipAddessList, addbufferlength, 10);
            if (error == 0)
            {
                string result = ipAddessList.ToString().Trim();
                if (result.Contains(' '))
                {
                    return result.Split(' ').ToList();
                }
                else
                {
                    return new List<string>() { result };
                }
            }
            return new List<string>();
        }


        /// <summary>
        /// 伺服使能打开
        /// </summary>
        /// <param name="axis">轴号</param>
        /// <returns></returns>
        public OperationResult ServeON(short axis)
        {
            var result = CommonInitValidate();
            if (result.IsSuccess == false) return result;
            //定义错误码
            int error = 0;
            try
            {
                error = zmcaux.ZAux_Direct_SetAxisEnable(handle, axis, 1);
                ErrorHandler("ZAux_Direct_SetAxisEnable", error);
            }
            catch (Exception ex)
            {

                result.IsSuccess = false;
                result.ErrorMsg = ex.Message;
                return result;
            }
            return OperationResult.CreatSuccessResult();

        }

        /// <summary>
        /// 伺服使能关闭
        /// </summary>
        /// <param name="axis">轴号</param>
        /// <returns></returns>
        public OperationResult ServeOFF(short axis)
        {
            var result = CommonInitValidate();
            if (result.IsSuccess == false) return result;
            //定义错误码
            int error = 0;
            try
            {
                error = zmcaux.ZAux_Direct_SetAxisEnable(handle, axis, 0);
                ErrorHandler("ZAux_Direct_SetAxisEnable", error);
            }
            catch (Exception ex)
            {

                result.IsSuccess = false;
                result.ErrorMsg = ex.Message;
                return result;
            }
            return OperationResult.CreatSuccessResult();

        }

        /// <summary>
        /// 读取使能状态
        /// </summary>
        /// <param name="axis"></param>
        /// <returns></returns>
        public OperationResult ReadServo(short axis)
        {
            var result = CommonInitValidate();
            if (result.IsSuccess == false) return result;
            //定义错误码
            int error = 0;
            int piValue = -1;
            try
            {
                error = zmcaux.ZAux_Direct_GetAxisEnable(handle, axis, ref piValue);
                ErrorHandler("ZAux_Direct_GetAxisEnable", error);
            }
            catch (Exception ex)
            {

                result.IsSuccess = false;
                result.ErrorMsg = ex.Message;
                return result;
            }
            if (piValue == 1)
            {
                return OperationResult.CreatSuccessResult();
            }
            else
            {
                result.IsSuccess = false;
                result.ErrorMsg = "   使能未开启";
                return result;
            }



        }

        /// <summary>
        /// 读取轴状态
        /// </summary>
        /// <param name="axis"></param>
        /// <returns></returns>
        public float ReadAxisStatus(short axis)
        {
            var result = CommonInitValidate();
            if (result.IsSuccess == false) return -999;
            //定义错误码
            int error = 0;
            int piValue = -1;
            try
            {
                error = zmcaux.ZAux_Direct_GetAxisStatus(handle, axis, ref piValue);
                ErrorHandler("ZAux_Direct_GetAxisStatus", error);
                return piValue;
            }
            catch (Exception ex)
            {

                result.IsSuccess = false;
                result.ErrorMsg = ex.Message;
                return -999;
            }



        }
        /// <summary>
        /// 报警复位
        /// </summary>
        /// <param name="axis"></param>
        /// <returns></returns>
        public OperationResult AlarmReset(short axis)
        {
            var result = CommonInitValidate();
            if (result.IsSuccess == false) return result;
            //定义错误码
            int error = 0;
            try
            {
                error = zmcaux.ZAux_BusCmd_DriveClear(handle, (uint)axis, 0);
                ErrorHandler("ZAux_BusCmd_DriveClear", error);
                return OperationResult.CreatSuccessResult();
            }
            catch (Exception ex)
            {

                result.IsSuccess = false;
                result.ErrorMsg = ex.Message;
                return result;
            }

        }

        /// <summary>
        /// 判断总线是否初始化成功及判断轴数
        /// </summary>
        /// <param name="busIni">判断总线是否初始化成功</param>
        /// <param name="busNodeNum">读取节点数</param>
        /// <param name="busAxisNum">读取轴数</param>
        /// <returns></returns>
        public OperationResult BusIniRead(out float busIni, out int busNodeNum, out float busAxisNum)
        {
            var result = CommonInitValidate();
            busIni = -1;
            busNodeNum = -1;
            busAxisNum = -1;
            if (result.IsSuccess == false) return result;
            //定义错误码
            int error = 0;
            try
            {
                error = zmcaux.ZAux_Direct_GetUserVar(handle, "Bus_InitStatus", ref busIni);               //读取BAS文件中的变量判断总线初始化完成状态
                error += zmcaux.ZAux_BusCmd_GetNodeNum(handle, 0, ref busNodeNum);                            //读取槽位0上节点个数。
                error += zmcaux.ZAux_Direct_GetUserVar(handle, "Bus_TotalAxisnum", ref busAxisNum);         //读取BAS文件中的变量判断扫描的总轴数

                ErrorHandler("ZAux_Direct_GetUserVar", error);
                return OperationResult.CreatSuccessResult();
            }
            catch (Exception ex)
            {

                result.IsSuccess = false;
                result.ErrorMsg = ex.Message;
                return result;
            }

        }
        //总线初始化
        public OperationResult IniFile()
        {
            int error = -1;
            float tempstatus = -1;
            try
            {
                error = zmcaux.ZAux_Direct_GetUserVar(handle, "Bus_InitStatus", ref tempstatus);
                if (tempstatus != -1)
                {
                    error = zmcaux.ZAux_BasDown(handle, CommendMethods.strFilePath, 1);   //下载到ROM
                    ErrorHandler("ZAux_BasDown", error);
                    Thread.Sleep(20);
                    StringBuilder buffer = new StringBuilder(10240);
                    error = zmcaux.ZAux_Execute(handle, "RUNTASK 1,Ecat_Init", buffer, 0);
                }
                return OperationResult.CreatSuccessResult();
            }
            catch (Exception ex)
            {
                return OperationResult.CreatFailResult();
            }



        }

        /// <summary>
        /// 设置DO输出的值
        /// </summary>
        /// <param name="adress">地址</param>
        /// <param name="value">值</param>
        /// <returns></returns>
        public OperationResult SetDO(int adress, ushort value)
        {
            var result = CommonInitValidate();
            if (result.IsSuccess == false) return result;
            //定义错误码
            int error = -1;
            try
            {

                error = zmcaux.ZAux_Direct_SetOp(handle, adress, value);
                ErrorHandler("ZAux_Direct_SetOp", error);
                return OperationResult.CreatSuccessResult();
            }
            catch (Exception ex)
            {

                result.IsSuccess = false;
                result.ErrorMsg = ex.Message;
                return result;
            }
        }

        /// <summary>
        /// 读取DO的值
        /// </summary>
        /// <param name="adress">地址</param>
        /// <returns>返回值</returns>
        public uint GetDO(int adress)
        {
            var result = CommonInitValidate();
            if (result.IsSuccess == false) return 999;
            //定义错误码
            int error = 0;
            uint value = 999;
            try
            {
                error = zmcaux.ZAux_Direct_GetOp(handle, adress, ref value);
                ErrorHandler("ZAux_Direct_GetOp", error);
                return value;
            }
            catch (Exception ex)
            {

                result.IsSuccess = false;
                result.ErrorMsg = ex.Message;
                return 999;
            }
        }

        /// <summary>
        /// 读取DI的值
        /// </summary>
        /// <param name="adress">地址</param>
        /// <returns>返回值</returns>
        public uint GetDI(int adress)
        {
            var result = CommonInitValidate();
            if (result.IsSuccess == false) return 999;
            //定义错误码
            int error = 0;
            uint value = 999;
            try
            {
                error = zmcaux.ZAux_Direct_GetIn(handle, adress, ref value);
                ErrorHandler("ZAux_Direct_GetIn", error);
                return value;
            }
            catch (Exception ex)
            {

                result.IsSuccess = false;
                result.ErrorMsg = ex.Message;
                return 999;
            }
        }

        /// <summary>
        /// SDO读取数据字典
        /// </summary>
        /// <param name="slot">槽位号</param>
        /// <param name="node">节点号</param>
        /// <param name="index">主索引</param>
        /// <param name="subindex">子索引</param>
        /// <param name="type">数据类型</param>
        /// <returns>返回值</returns>
        public int SDOREAD(uint slot, uint node, uint index, uint subindex, uint type)
        {
            var result = CommonInitValidate();
            if (result.IsSuccess == false) return 0;
            //定义错误码
            int error = 0;
            int value = 0;
            try
            {
                error = zmcaux.ZAux_BusCmd_SDORead(handle, slot, node, index, subindex, type, ref value);
                ErrorHandler("ZAux_BusCmd_SDORead", error);
                return value;
            }
            catch (Exception ex)
            {

                result.IsSuccess = false;
                result.ErrorMsg = ex.Message;
                return 0;
            }
        }

        /// <summary>
        /// 伺服回零 原点开关接在控制器上 先控制器回零，再用驱动器直接回零。
        /// </summary>
        /// <param name="axis">轴号</param>
        /// <param name="DIHome">原点开关信号</param>
        /// <param name="dir">回零方向</param>
        /// <returns></returns>
        public OperationResult Home(short axis, int DIHome, int dir)
        {
            //判断是否满足运动条件
            var result = CommonMotionValidate(axis);
            if (result.IsSuccess == false) return result;

            //创建错误码
            int error = 0;
            int runstate = 0;
            uint homeStatus = 0;
            try
            {
                //设置脉冲当量

                //设置脉冲当量
                switch (axis)
                {
                    case 0:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin0);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 1:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin1);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 2:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin2);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 3:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin3);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 4:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin4);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 5:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin5);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 6:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin6);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    case 7:
                        error = zmcaux.ZAux_Direct_SetUnits(handle, axis, unin7);
                        ErrorHandler("ZAux_Direct_SetUnits", error);
                        break;
                    default:
                        break;
                }
                //设置运行速度
                error = zmcaux.ZAux_Direct_SetSpeed(handle, axis, 10);
                //设置加速度
                error = zmcaux.ZAux_Direct_SetAccel(handle, axis, 50);
                //设置减速度
                error = zmcaux.ZAux_Direct_SetDecel(handle, axis, 50);
                //设置爬行速度
                error = zmcaux.ZAux_Direct_SetCreep(handle, axis, 5);
                //设置HomeIO
                error = zmcaux.ZAux_Direct_SetDatumIn(handle, axis, DIHome);

                //执行回原点

                //控制器回零 3正转回零，4反转回零
                error = zmcaux.ZAux_Direct_Single_Datum(handle, axis, dir);
                do
                {
                    error = zmcaux.ZAux_Direct_GetIfIdle(handle, axis, ref runstate);
                    if (runstate == 0)
                    {
                        Thread.Sleep(100);
                    }
                    else
                    {
                        break;
                    }
                } while (true);

                do
                {
                    error = zmcaux.ZAux_BusCmd_GetHomeStatus(handle, (uint)axis, ref homeStatus);
                    if (homeStatus == 0)
                    {
                        Thread.Sleep(100);
                    }
                    else
                    {
                        break;
                    }
                } while (true);

                //总线回零 35 直接设原点
                error = zmcaux.ZAux_BusCmd_Datum(handle, (uint)axis, 35);
                //手动清零反馈位置Mpos
                error = zmcaux.ZAux_Direct_SetMpos(handle, axis, 0.0f);
                return OperationResult.CreatSuccessResult();

            }
            catch (Exception ex)
            {

                result.IsSuccess = false;
                result.ErrorMsg = ex.Message + "回原点失败";
                return result;
            }
        }

        //总线直接设原点
        public OperationResult BusSetZero(short axis)
        {
            var result = CommonMotionValidate(axis);
            if (result.IsSuccess == false) return result;

            //创建错误码
            int error = 0;
            int q=zmcaux.ZAux_BusCmd_Datum(handle, (uint)axis, 35);
            ErrorHandler("ZAux_BusCmd_Datum", error);
            Thread.Sleep(100);
            zmcaux.ZAux_Direct_SetMpos(handle, axis, 0.0f);
            return OperationResult.CreatSuccessResult();

        }

        /// <summary>
        /// 设置正极限
        /// </summary>
        /// <param name="axis">轴号</param>
        /// <param name="fsLimit">正向限位值</param>
        /// <returns></returns>
        public OperationResult SetFsLimit(short axis, float fsLimit)
        {
            var result = CommonInitValidate();
            if (result.IsSuccess == false) return result;
            //定义错误码
            int error = 0;
            try
            {
                error = zmcaux.ZAux_Direct_SetFsLimit(handle, axis, fsLimit);
                ErrorHandler("ZAux_Direct_SetFsLimit", error);
                return OperationResult.CreatSuccessResult();
            }
            catch (Exception ex)
            {

                result.IsSuccess = false;
                result.ErrorMsg = ex.Message;
                return result;
            }

        }

        /// <summary>
        /// 设置负极限
        /// </summary>
        /// <param name="axis">轴号</param>
        /// <param name="rsLimit">负向限位值</param>
        /// <returns></returns>
        public OperationResult SetRsLimit(short axis, float rsLimit)
        {
            var result = CommonInitValidate();
            if (result.IsSuccess == false) return result;
            //定义错误码
            int error = 0;
            try
            {
                error = zmcaux.ZAux_Direct_SetRsLimit(handle, axis, rsLimit);
                ErrorHandler("ZAux_Direct_SetFsLimit", error);
                return OperationResult.CreatSuccessResult();
            }
            catch (Exception ex)
            {

                result.IsSuccess = false;
                result.ErrorMsg = ex.Message;
                return result;
            }

        }



        /// <summary>
        /// 通用的运动验证 验证初始化以及是否在运动 使能
        /// </summary>
        /// <param name="axis">轴号</param>
        /// <returns>结果</returns>
        private OperationResult CommonMotionValidate(short axis)
        {
            OperationResult result = CommonInitValidate();
            //判断初始化是否成功
            if (result.IsSuccess == false)
            {
                return result;
            }
            result = ReadServo(axis);
            if (result.IsSuccess == false)
            {
                return result;
            }
            //判断是否正在运行
            if (IsMoving(axis))
            {
                result.IsSuccess = false;
                result.ErrorMsg = "轴正在运行";
                return result;
            }

            return OperationResult.CreatSuccessResult();



        }


        /// <summary>
        /// 通用的运动验证 验证初始化以及是否在运动
        /// </summary>
        /// <param name="axis">轴号</param>
        /// <returns>结果</returns>
        private OperationResult CommonMotionJudge(short axis)
        {
            OperationResult result = CommonInitValidate();
            //判断初始化是否成功
            if (result.IsSuccess == false)
            {
                return result;
            }

            //判断是否正在运行
            if (IsMoving(axis))
            {
                result.IsSuccess = false;
                result.ErrorMsg = "轴正在运行";
                return result;
            }

            return OperationResult.CreatSuccessResult();



        }


        //通用初始化验证
        public OperationResult CommonInitValidate()
        {
            OperationResult result = new OperationResult();
            if (InitedOK == false)
            {
                result.IsSuccess = false;
                result.ErrorMsg = "初始化未成功";
                return OperationResult.CreatFailResult();
            }
            else
            {
                return OperationResult.CreatSuccessResult();
            }
        }

        //判断某个轴是否正在运行
        public bool IsMoving(short axis)
        {
            OperationResult result = CommonInitValidate();
            if (result.IsSuccess == false)
            {
                return false;
            }
            //定义运动状态
            int runstate = -1;
            //定义错误码
            int error = 0;
            //获取轴状态
            try
            {
                //runstate 0没有运动完成 1运动完成
                error = zmcaux.ZAux_Direct_GetIfIdle(handle, axis, ref runstate);
                //错误码验证
                ErrorHandler("ZAux_Direct_GetIfIdle", error);

                return runstate == 0;
            }
            catch (Exception)
            {
                //正在运动
                return true;
            }


        }

        /// <summary>
        /// 错误处理
        /// </summary>
        /// <param name="commend">执行命令</param>
        /// <param name="error">错误码</param>
        private void ErrorHandler(string commend, int error)
        {
            string result = string.Empty;
            switch (error)
            {
                case 0: break;
                default:
                    result = string.Format("{0}" + "指令执行错误，错误码为{1}", commend, error);
                    break;
            }
            if (result.Length > 0)
            {
                throw new Exception(result);
            }

        }



    }

    /// <summary>
    /// 操作结果类
    /// </summary>
    /// <returns></returns>
    public class OperationResult
    {
        //是否成功
        public bool IsSuccess { get; set; }
        //错误信息
        public string ErrorMsg { get; set; }


        public static OperationResult CreatSuccessResult()
        {
            return new OperationResult()
            {
                IsSuccess = true,
                ErrorMsg = "Success"
            };
        }

        public static OperationResult CreatFailResult()
        {
            return new OperationResult()
            {
                IsSuccess = false,
                ErrorMsg = "Fail"
            };
        }


    }
}
